﻿using QG.FrontDesk.BLL.IService;
using QG.FrontDesk.DAL.IRepository;
using QG.FrontDesk.Model;
using QG.FrontDesk.Remote;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QG.FrontDesk.BLL.Service
{
    public class VerificationCodeService : IVerificationCodeService
    {
        private IVerificationCodeRepository verificationCodeRepository;
        public VerificationCodeService(IVerificationCodeRepository verificationCodeRepository)
        {
            this.verificationCodeRepository = verificationCodeRepository;
        }
        /// <summary>
        /// 新增验证消息
        /// </summary>
        /// <returns></returns>
        public int AddCode(string code, string email)
        {
            VerificationCode verificationCode = new VerificationCode()
            {
                CodeNum = code,
                Email = email,
                ValidTime = DateTime.Now,
                LoseTime = DateTime.Now.AddMinutes(10)//十分钟之内有效
            };
            return verificationCodeRepository.AddCode(verificationCode);
        }
        /// <summary>
        /// 得到有效验证码
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public string GetCode(string email)
        {
            VerificationCode verificationCode = new VerificationCode()
            {
                Email = email
            };
            return verificationCodeRepository.GetCode(verificationCode);
        }
        /// <summary>
        /// 查询验证码
        /// </summary>
        /// <param name="verificationCode"></param>
        /// <returns></returns>
        public int QueryVerificationCode(VerificationCode verificationCode)
        {
            return verificationCodeRepository.QueryVerificationCode(verificationCode);
        }
        /// <summary>
        /// 发送验证码与添加验证码到数据库
        /// </summary>
        /// <param name="telphone"></param>
        /// <returns></returns>
        public bool SendCode(string telphone)
        {

            //new第三方类 
            TenXunYunSMS tenXunYunSMS = new TenXunYunSMS();
            //设置开发者账号
            tenXunYunSMS.appId = Convert.ToInt32(ConfigurationManager.AppSettings["appId"]);
            tenXunYunSMS.appKey = ConfigurationManager.AppSettings["appKey"];

            string code = verificationCodeRepository.GetCode(new VerificationCode()
            {
                Phone = telphone
            });
            //如果数据库存在,获取该值,保证5分钟有效
            if (!string.IsNullOrEmpty(code))
            {
                tenXunYunSMS.Code = Convert.ToInt32(code);
            }
            else//新增一条记录
            {
                tenXunYunSMS.Code = new Random().Next(100000, 999999);//随机产生6位验证码
                VerificationCode verificationCode = new VerificationCode();
                verificationCode.CodeNum = tenXunYunSMS.Code.ToString();
                verificationCode.Phone = telphone;
                verificationCode.ValidTime = DateTime.Now;
                verificationCode.LoseTime = DateTime.Now.AddMinutes(5);//5分钟之内有效
                verificationCodeRepository.AddVerificationCode(verificationCode);
            }

            //发送验证码
            return tenXunYunSMS.SetSMS(telphone);

        }
    }
}
